package com.lhl.aliyun.oss;

import cn.hutool.json.JSONUtil;
import com.aliyun.oss.model.ListObjectsV2Result;
import com.aliyun.oss.model.OSSObjectSummary;
import com.lhl.aliyun.autoconfigure.oss.OssAutoConfiguration;
import com.lhl.aliyun.autoconfigure.oss.OssFileUtil;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.net.URLDecoder;
import java.util.List;
import java.util.UUID;


@SpringBootTest(classes = OssAutoConfiguration.class)
public class OssAutoConfigurationDemo {
    @Autowired
    private OssFileUtil ossFileUtil;
    @Test
    public void 上传照片Utils() throws Exception {
    	final String localFile = "C:\\app\\尺码建议表\\xf.jpg";
    	String url = ossFileUtil.upLoadFileToALiyun("cmb/xf.jpg", new File(localFile));
        System.out.println(url);
    }
    @Test
    public void 上传照片Utils1() throws Exception {
        final String localFile = "C:\\dev\\javaDev\\2021Dev\\123.jpg";
        ossFileUtil.upLoadFileToALiyun("柒牌.jpg", new File(localFile));

        ossFileUtil.downloadFileFromALiyun("https://qipai7-file.oss-cn-beijing.aliyuncs.com/柒牌.jpg", "C:\\dev\\javaDev\\");
    }

    @Test
    public void 上传Utils() throws Exception {
    	final String fileKey = UUID.randomUUID().toString()+"/111.wmv";
    	System.out.println("fileKey=="+fileKey);
    	final String localFile = "C:\\dev\\javaDev\\2021Dev\\111.wmv";
    	ossFileUtil.upLoadFileToALiyun(fileKey, new File(localFile));
    }
    @Test
    public void 下载Utils() throws Exception {
    	final String localFile = "888.wmv";
    	ossFileUtil.downloadFileFromALiyun("0095eed9-2328-43b2-8b9c-7cafecd9bfb8.jpg", "C:\\app\\20220315\\aaa.jpg");
    }
    @Test
    public void 下载图片Utils() throws Exception {
    	String localFile = URLDecoder.decode("https://qipai7-file.oss-cn-beijing.aliyuncs.com/%E6%9F%92%E7%89%8C.jpg", "UTF-8");
//    	String localFile = new String("https://qipai7-file.oss-cn-beijing.aliyuncs.com/柒牌.jpg".getBytes(), "UTF-8");
    	ossFileUtil.downloadFileFromALiyun(localFile, "C:\\dev\\javaDev\\2021Dev\\");
    }
    @Test
    public void 字符串替换() throws Exception {
    	String a = "123sbc";
    	String file = StringUtils.remove("https://qipai7-file.oss-cn-beijing.aliyuncs.com/b5c668b8-9afc-4966-b6e5-414e5bed9d21/111.wmv", "https://qipai7-file.oss-cn-beijing.aliyuncs.com/");
    	System.out.println("file=="+file);
    }
    @Test
    public void 获取目录下文件列表() throws Exception {
        ListObjectsV2Result result = ossFileUtil.getOSSClient().listObjectsV2("qplife-image","mainImage/721A7351058");
        List<OSSObjectSummary> ossObjectSummaries = result.getObjectSummaries();

        for (OSSObjectSummary s : ossObjectSummaries) {
            System.out.println("\t" + s.getKey());
        }
    }

    @Test
    public void 获取目录下文件列表2() throws Exception {
        List<String> ps = ossFileUtil.listObjectsV2("qplife-image","mainImage/722A7088005");
        System.out.println("=====================================");
        System.out.println(JSONUtil.toJsonPrettyStr(ps));
    }
    @Test
    public void 获取目录下文件列表3() throws Exception {
        List<String> ps = ossFileUtil.listObjectsV2("qipai7","ios");
        System.out.println(JSONUtil.toJsonPrettyStr(ps));
    }

    /*@Test
    public void 上传() throws Exception {
        final String bucketName = "qipai7-file";
        final String fileKey = UUID.randomUUID().toString()+"/03-PMO_大会员项目一期验收-2018.pptx";
        final String localFile = "C:\\dev\\03-PMO_大会员项目一期验收-2018.pptx";
        this.ossClient.putObject(bucketName, fileKey, new File(localFile));
        System.out.println(this.ossClient.generatePresignedUrl(bucketName, fileKey, DateUtils.addSeconds(new Date(), 30), HttpMethod.GET).toString());
    }
    @Test
    public void 下载() throws Exception {
    	final String bucketName = "qipai7-file";
    	// 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
    	
    	ossClient.getObject(new GetObjectRequest(bucketName, 
    			"b31f7490-dddf-4f2a-9dc4-4d0039e1ea1f/03-PMO_大会员项目一期验收-2018.pptx"), new File("C:\\dev\\javaDev\\2021Dev"));
    }*/
    
}
